<template>
  <div>
    <h3>{{ $t("business.network.ingress_router") }}</h3>
    <el-alert v-if="ingressRules.length === 0" type="info">
      <template v-slot:title>
        <div>
          <i class="el-icon-info" />
          <span>
            {{
              $t("business.network.ingress_detail_help", [resourceType, name])
            }}
          </span>
        </div>
      </template>
    </el-alert>
    <div v-for="(item, index) in ingressRules" :key="index">
      <span>{{ $t("business.network.host") }}: {{ item.host }}</span>
      <complex-table v-if="ingressRules.length !== 0" :data="item.http?.paths">
        <el-table-column
          :label="$t('business.network.path_type')"
          prop="pathType"
        />
        <el-table-column :label="$t('business.network.path')" prop="path" />
        <el-table-column
          :label="$t('business.workload.name')"
          prop="backend.service.name"
        />
        <el-table-column
          :label="$t('business.workload.port')"
          prop="backend.service.port.number"
        />
      </complex-table>
      <br /><br />
    </div>
  </div>
</template>

<script setup lang="ts">
import ComplexTable from "@/views/domain/kubeHud/views/components/complex-table/index.vue";
import { useI18n } from "vue-i18n";
import { Ingress, IngressRule, useK8SApi } from "../../../api/k8s";
import { onMounted, ref, watch } from "vue";
import { checkPermissions } from "../../../utils/permission";
// import { listIngressWithNs } from "@/api/ingress";
// import { checkPermissions } from "@/utils/permission";
const { ingressRepo } = useK8SApi();
const { t } = useI18n();

type Prop = {
  cluster?: string;
  namespace?: string;
  name?: string;
  resourceType?: string;
};
const props = defineProps<Prop>();

const ingressRules = ref<IngressRule[]>([]);
const loading = ref(false);

const reloadIngressList = async () => {
  loading.value = true;
  const hasPermission = await checkPermissions({
    scope: "namespace",
    apiGroup: "",
    resource: "ingresses",
    verb: "list"
  });

  if (!hasPermission) {
    return;
  }
  ingressRepo
    .getIngress({
      clusterName: props.cluster,
      namespace: props.namespace
    })
    .then(res => {
      ingressRules.value = res.items?.flatMap(i => i.spec.rules || []);
    });
};

onMounted(() => {
  reloadIngressList();
});
</script>

<style scoped></style>
